package com.uchedao.mybaselibrary.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.baidu.mobstat.StatService;
import com.uchedao.mybaselibrary.http.VolleyUtil;
import com.uchedao.mybaselibrary.utils.LogUtil;
import com.uchedao.mybaselibrary.utils.NetUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * 基础页面
 * Created by zhengxizhen on 15/8/10.
 */
public abstract class BaseActivity extends Activity {

    /**
     * 页面标签
     */
    protected String TAG = "BaseActivity";
    /**
     *
     * */
    protected RequestQueue mQueue;

    /**
     * 设置页面标签
     */
    protected abstract String getActivityTag();

    /**
     * 获取布局XML ID
     */
    protected abstract int getLayoutResID();

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化监听事件
     */
    protected abstract void initListener();

    protected void onSetting() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onSetting();
        setContentView(getLayoutResID());
        //获取单例网络请求队列
        mQueue = VolleyUtil.getInstance(this).getRequestQueue(this);
        TAG = getActivityTag();
        init();
    }

    protected void init() {
        initView();
        initData();
        initListener();
        setDeBug();
    }

    /**
     * 设置日志系统
     */
    private void setDeBug() {
        LogUtil.isDebug = true;
    }

    @Override
    protected void onPause() {
        MobclickAgent.onPause(this);
        StatService.onPageEnd(this, TAG);
        //取消所有请求
        if (!TextUtils.isEmpty(TAG)) {
            mQueue.cancelAll(TAG);
        }
        super.onPause();
    }

    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this, TAG, null);
        StatService.onPageStart(this, TAG);
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 添加网络请求
     */
    public void addQueue(Request request) {
        if (!TextUtils.isEmpty(TAG)) {
            if (NetUtil.isNet(this) && request != null) {
                request.setTag(TAG);
                mQueue.add(request);
            } else {
                mQueue.cancelAll(TAG);
            }
        }
    }

}
